package com.cloud.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.core.BaseService;
import com.cloud.dao.ContactBean;
import com.cloud.dao.CrcBean;
import com.cloud.dao.DoctorBean;
import com.cloud.dao.PatientBean;
import com.cloud.dao.UserBean;
import com.cloud.dao.impl.UserDao;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-11-4
 * @version 1.0 
 */
@Service("UserService")
public class UserService extends BaseService{
	private static final Logger logger = LoggerFactory.getLogger(UserService.class);
	
	@Autowired
	private UserDao UserDao;
	@Autowired
	private DoctorService DoctorService;
	@Autowired
	private PatientService PatientService;
	@Autowired
	private CrcService CrcService;
	
	/**
	 * 查询用户
	 * @param bean
	 * @return
	 */
	public List<UserBean> getUserList(UserBean bean){
		logger.debug(String.format("开始查询用户[%s]的信息.", bean.getUserName()));
		return UserDao.getUserList(bean);
	}
	
	/**
	 * 添加用户
	 * @param bean
	 */
	public void addUser(UserBean bean){
		logger.debug(String.format("开始添加用户[%s]的信息.", bean.getUserName()));
		int userId = UserDao.addUser(bean);
		if(userId == -1)
			return;
		if(CommUtil.PATIENT == Integer.valueOf(bean.getClientType())){
			PatientBean patient = new PatientBean();
			patient.setId(String.valueOf(userId));
			PatientService.addPatient(patient);
		}
		if(CommUtil.DOCTOR == Integer.valueOf(bean.getClientType())){
			DoctorBean doctor = new DoctorBean();
			doctor.setId(String.valueOf(userId));
			DoctorService.addDoctor(doctor);
		}
		if(CommUtil.CRC == Integer.valueOf(bean.getClientType())){
			CrcBean crc = new CrcBean();
			crc.setId(String.valueOf(userId));
			CrcService.insertCrc(crc);
		}
	}
	
	/**
	 * 修改用户
	 * @param bean
	 * @param flag
	 */
	public void updateUser(UserBean bean, boolean flag){
		logger.debug(String.format("开始修改用户[%s]的信息.", bean.getUserId()));
		UserDao.updateUser(bean, flag);
	}
	
	/**
	 * 查询联系人
	 * @param bean
	 * @param isNotice
	 * @return
	 */
	public List<ContactBean> getUserContactList(ContactBean bean, boolean isNotice){
		logger.debug(String.format("开始查询用户[%s]的联系人信息.", bean.getUserId()));
		return UserDao.getUserContactList(bean, isNotice);
	}
	
	/**
	 * 添加联系人
	 * @param bean
	 */
	public void addUserContact(List<ContactBean> beans){
		for(ContactBean bean : beans){
			logger.debug(String.format("开始添加用户[%s]的联系人信息.", bean.getUserId()));
			UserDao.addUserContact(bean);
		}
	}
	
	/**
	 * 修改联系人
	 * @param bean
	 */
	public void updateUserContact(ContactBean bean){
		logger.debug(String.format("开始修改用户[%s]的联系人信息.", bean.getUserId()));
		UserDao.updateUserContact(bean);
	}
	
	public Integer contactsCont(ContactBean bean){
		return UserDao.contactsCont(bean);
	}
}
